var obj = DDLogin({
    id:"login_container",//这里需要你在自己的页面定义一个HTML标签并设置id，例如<div id="login_container"></div>或<span id="login_container"></span>
    goto: encodeURIComponent("https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=appid&response_type" +
        "=code&scope=snsapi_login&state=STATE&redirect_uri=http://xumy.com/login"),
    width : "365",
    height: "400"
});

var hanndleMessage = function (event) {
    var origin = event.origin;
    console.log("origin", event.origin);
    if( origin == "https://login.dingtalk.com" ) { //判断是否来自ddLogin扫码事件。
        var loginTmpCode = event.data; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
        console.log("loginTmpCode", loginTmpCode);
        location.href='https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=appId&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=编译后的url&loginTmpCode='+loginTmpCode+''
    }
};

if (typeof window.addEventListener != 'undefined') {
    window.addEventListener('message', hanndleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
    window.attachEvent('onmessage', hanndleMessage);
}

// 默认显示扫码登录
$('.password').attr('src','')
$('.password').hide()
// 点击扫码登录
function goCode(){
    $('.code').show()
    $('.codeBtn').addClass('active')
    $('.password').attr('src','')
    $('.passwordBtn').removeClass('active')
}
// 点击密码登录
function goPassword(){
    $('.password').show()
    $('.password').attr('src','https://login.dingtalk.com/login/index.htm?goto=https%3A%2F%2Foapi.dingtalk.com%2Fconnect%2Foauth2%2Fsns_authorize%3Fappid%3Ddingoavfji6kojhvilezvv%26response_type%3Dcode%26scope%3Dsnsapi_login%26state%3DSTATE%26redirect_uri=http:/xumy.com/login')
    $('.passwordBtn').addClass('active')
    $('.code').hide()
    $('.codeBtn').removeClass('active')
}


function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
if(window.location.search != ''){
    var code = getQueryString('code')
    $.ajax({
        url: "/api/login/userLogin",
        type: "get",
        data:{code:code},
        dataType: "json",
        success: function (data) {
            if(data.status == 0){
                localStorage.setItem('tang', JSON.stringify({
                    ti: Date.now(),
                    to: data.responseObject.publicKey,
                    user: data.responseObject.user
                }));
                window.location.href = "/main_baseMain"
            }else{
                layer.msg('登录失败', {
                    time: 2000,
                    icon: 5,
                    shade: [0.5, '#000']
                });
                location.reload()
            }
        }
    })
}

